package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common;

import com.google.common.base.Optional;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.project.ProjectManager;
import edu.stanford.bmir.protege.web.client.ui.library.progress.ProgressMonitor;
import edu.stanford.bmir.protege.web.shared.project.ProjectDetails;

public class GWTExtUtil {
	public final static String EVENT_ONSELECTIONCHANGE = "STKOSONSELECTIONCHANGE";
	public final static String EVENT_ONCONTEXTMENU = "STKOSONCONTEXTMENU";
	public final static String EVENT_ONDBLCLICK = "STKOSONDBLCLICK";
	public final static String EVENT_ONCLICK = "STKOSONCLICK";
	public final static String EVENT_ONEXPANDNODE = "STKOSONEXPANDNODE";
	public final static String DEFAULTLISTBOXITEMVALUEID = "defaultListBoxItemValueID";
	public static final String ENRICHERBLUECLS = "stkosutil-enricher-blue";
	public static final String MAPPERNUMBERCIRCLECLS = "stkosutil-mapperNumber-circle";

	public static void setTreeNodeIconCls(TreeNode node, String iconCls) {
		if (!node.isRendered()) {
			// JavaScriptObjectHelper.setAttribute(node.con configJS, "iconCls",
			// iconCls);
			node.setIconCls(iconCls);
		} else {
			com.google.gwt.user.client.Element el = node.getUI().getIconEl();
			if (el != null) {
				DOM.setElementAttribute(el, "className", "x-tree-node-icon "
						+ iconCls);
				DOM.setElementAttribute(el, "class", "x-tree-node-icon "
						+ iconCls);
			}
		}
	}

	public static void setTreeNodeCls(TreeNode node, String cls) {
		cls = cls == null ? "" : cls.trim();
		if ("".equals(cls)) {
			return;
		}
		node.setCls(cls);
		if (node.isRendered()) {
			com.google.gwt.user.client.Element el = node.getUI().getElNode();
			if (el != null) {
				el.addClassName(cls);
			}
		}
	}

	public static void removeTreeNodeCls(TreeNode node, String cls) {
		if (node == null) {
			return;
		}
		cls = cls == null ? "" : cls.trim();
		if ("".equals(cls)) {
			return;
		}
		if (!node.isRendered()) {
			node.setCls(null);
		} else {
			com.google.gwt.user.client.Element el = node.getUI().getElNode();
			if (el != null) {
				el.removeClassName(cls);
			}
		}
	}

	/**
	 * create mapper number label
	 * 
	 * @param number
	 * @return
	 */
	public static String createNodeMapperNumberLabel(int number) {
		String mapper = "<font class=\"" + MAPPERNUMBERCIRCLECLS
				+ "\" unselectable=\"on\" qtip=\"映射号：" + number + "\">"
				+ number + "</font>";
		return mapper;
	}

	public static String createNodeEnrichedFlagLabel(String browserText) {
		String enriched = "<font class=\"" + ENRICHERBLUECLS + "\">"
				+ browserText + "</font>";
		return enriched;
	}

	public static String createNodeMergedFlagLabel(String browserText) {
		String merged = "<font class=\"" + ENRICHERBLUECLS + "\">"
				+ browserText + "</font>";
		return merged;
	}

	/**
	 * get project by projectDetails
	 * 
	 * @param projectDetails
	 * @param callback
	 */
	public static void getProject(final ProjectDetails projectDetails,
			final AsyncCallback<Project> callback) {
		if (projectDetails == null || projectDetails.getProjectId() == null
				|| callback == null) {
			GWT.log(GWTExtUtil.class.getName() + " --- getProject Failure!");
			return;
		}

		Optional<Project> op = ProjectManager.get().getProject(
				projectDetails.getProjectId());

		if (op.asSet().size() == 0) {
			ProjectManager.get().loadProject(projectDetails.getProjectId(),
					new AsyncCallback<Project>() {
						@Override
						public void onFailure(Throwable caught) {
							callback.onFailure(caught);
							GWT.log("Loading Project Failure!", caught);
						}

						@Override
						public void onSuccess(Project result) {
							if (result != null) {
								callback.onSuccess(result);
							} else {
								Throwable caught = new Throwable("The project "
										+ projectDetails.getDisplayName()
										+ " is non-existent!");
								callback.onFailure(caught);
								GWT.log("Loading Project Failure!", caught);
							}
						}
					});
		} else {
			callback.onSuccess(op.get());
		}
	}

	/**
	 * format string. </br> You should do like this: </br>
	 * GWTExtUtil.format("He{0}lo Wo{1}ld!","l","r"); </br> This code will
	 * return a string "Hello World!".
	 * 
	 * @param provider
	 * @param args
	 * @return
	 */
	public static String stringFormat(String provider, String... args) {
		String s = provider;
		if (s != null && (args != null) && (args.length > 0)) {
			for (int i = 0; i < args.length; i++) {
				String regex = "{" + i + "}";
				String replacement = args[i];
				s = s.replace(regex, replacement);
			}
		}
		return s;
	}

	public static boolean stringIsBlank(String arg0) {
		boolean flag = true;
		flag = arg0 == null ? true : "".equals(arg0.trim());
		return flag;
	}

	public static String stringTrim(String arg0) {
		String s = arg0;
		if (s != null) {
			s = s.trim();
		}
		return s;
	}

	public static void showProgressMonitor(String title, String msg) {
		title = title == null ? "" : title;
		msg = msg == null ? "" : msg;
		ProgressMonitor.get().showProgressMonitor(title, msg);
	}

	public static void hideProgressMonitor(String title, String msg, int timeout) {
		title = title == null ? "" : title;
		msg = msg == null ? "" : msg;
		timeout = timeout < 0 ? 0 : timeout;
		
		ProgressMonitor.get().showProgressMonitor(title, msg);
		Timer timer = new Timer() {
			@Override
			public void run() {
				ProgressMonitor.get().hideProgressMonitor();
			}
		};
		timer.schedule(timeout);
	}
}
